﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GenerateDungeon : MonoBehaviour {

    public int numSplits = 4;
    public int dungeonSize = 64;
    public int pieceSize = 10;

    public int minPartitionDimensions = 10;
    public float minSplitRatio = 0.4f;
    public float maxSplitRatio = 0.6f;
    public int minRoomMargin = 1;
    public float minRoomDimensions = 0.55f;
    public float maxRoomDimensions = 0.9f;

    private Dungeon dungeon;

    // Use this for initialization
	void Start () {
	
        // Generate a 16 room dungeon
        InitializeDungeon(dungeonSize);
        for (int i = 0; i < numSplits; i++)
        {
            print("splitting");
            dungeon.SplitAll();
            dungeon.GenerateRooms();
            print(dungeon.Rooms.Count.ToString() + " rooms generated");
        }
        dungeon.GenerateRooms();
        dungeon.GenerateCorridors();

        print(dungeon.Rooms.Count.ToString() + " rooms generated");

        DungeonIndexer indexer = new DungeonIndexer(dungeon);

        Dictionary<int, TileDescriptor> descriptors = indexer.TileDescriptors;
        foreach (int tileIndex in descriptors.Keys)
        {
            int logicalX = tileIndex % dungeonSize;
            int logicalY = tileIndex / dungeonSize;

            float tileX = logicalX * pieceSize + (float)pieceSize / 2;
            float tileZ = -logicalY * pieceSize - (float)pieceSize / 2;

            // Fetch!
            string resourceName = descriptors[tileIndex].ToString();
            print("loading resource " + resourceName);
            GameObject tile = Instantiate(Resources.Load("kitPieces/" + resourceName, typeof(GameObject))) as GameObject;
            tile.transform.position = new Vector3(tileX, 0, tileZ);
        }

        // Place the character controller in the last room
        GameObject characterController = GameObject.Find("First Person Controller");

        Room lastRoom = dungeon.Rooms[dungeon.Rooms.Count - 1];
        int lastRoomLogicalX = lastRoom.X;
        int lastRoomLogicalY = lastRoom.Y;
        int lastRoomLogicalWidth = lastRoom.Width;
        int lastRoomLogicalHeight = lastRoom.Height;
        int lastRoomLogicalCenterX = lastRoomLogicalX + lastRoomLogicalWidth / 2;
        int lastRoomLogicalCenterY = lastRoomLogicalY + lastRoomLogicalHeight / 2;

        float characterControllerX = lastRoomLogicalCenterX * pieceSize + (float)pieceSize / 2;
        float characterControllerZ = -lastRoomLogicalCenterY * pieceSize - (float)pieceSize / 2;

        characterController.transform.position = new Vector3(characterControllerX, 1.1f, characterControllerZ);

	}
	
	// Update is called once per frame
	void Update () {
	
	}

    private void InitializeDungeon(int size)
    {
        print("initializing new dungeon");
        dungeon = new Dungeon(size, minPartitionDimensions, minSplitRatio, maxSplitRatio, minRoomMargin, minRoomDimensions, maxRoomDimensions);
    }
}
